﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KabiManagementWeb.Models;
namespace KabiManagementWeb.Controllers
{
    public class CustomerController : Controller
    {
        KBManagementDataContext context = DBUtils.GetContext(DBUtils.DBType.Local);
        
        #region Customer
        #region View
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            ViewData.Add("customerType", new SelectList(context.Customer_Types.ToList<Customer_Type>(), "ID", "Ct_Name"));

            var customers = from cus in context.Customers select cus;
            return View(customers);
        }

        //
        //Post:/Customer/5
        [HttpPost]
        public ActionResult Index(string customerType)
        {
            ViewData.Add("customerType", new SelectList(context.Customer_Types.ToList<Customer_Type>(), "ID", "Ct_Name"));

            var customers = from cus in context.Customers where cus.CustomeTypeID == customerType select cus;
            return View(customers);
        }
        #endregion
        #region Details
        //
        // GET: /Customer/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        #endregion
        #region Create
        //
        // GET: /Customer/Create

        public ActionResult Create()
        {

            ViewData["CustomerType"] = new SelectList(context.Customer_Types.ToList<Customer_Type>(), "ID", "Ct_name");
            return View();
        }

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(Customer customer)
        {
            try
            {
                // TODO: Add insert logic here
                context.Customers.InsertOnSubmit(customer);
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion
        #region Edit

        //
        // GET: /Customer/Edit/5

        public ActionResult Edit(int id)
        {
            ViewData["CustomerType"] = new SelectList(context.Customer_Types.ToList<Customer_Type>(), "ID", "Ct_name");
            var cus = context.Customers.Single(x => x.ID == id);

            return View(cus);
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Customer customer)
        {
            try
            {
                // TODO: Add update logic here
                var cus = context.Customers.Single(x => x.ID == id);
                cus.CustomeTypeID = customer.CustomeTypeID;
                cus.Email = customer.Email;
                cus.FullName = customer.FullName;
                cus.Phone = customer.Phone;

                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion
        #region Delete


        //
        // GET: /Customer/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion

        #endregion


    }
}
